package com.yonyou.cyxdms.sysAuth.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyxdms.sysAuth.service.dto.BindDTO;
import com.yonyou.cyxdms.sysAuth.service.entity.BindPO;
import com.yonyou.cyxdms.sysAuth.service.service.dudu.BindService;
import com.yonyou.cyxdms.sysAuth.service.service.manage.employee.EmployeeService;
import com.yonyou.cyxdms.sysAuth.service.vo.employee.EmployeeSearBindVO;
import com.yonyou.cyxdms.sysAuth.service.vo.employee.SearchBindParamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author lijun
 * @since 2019-11-07
 */
@Api(tags = "嘟嘟绑定相关接口")
@RestController
@RequestMapping("/bind")
public class BindController extends BaseController {

    @Autowired
    BindService bindService;

    @Autowired
    EmployeeService employeeService;

    /**
     * 分页查询数据
     *
     * @param employeeName 员工姓名
     * @param employeeNo   员工编号
     * @param extension    分机号
     * @param currentPage  页数
     * @param pageSize     分页大小
     * @return com.baomidou.mybatisplus.core.metadata.IPage 分页对象
     * @author lijun
     * @since 2019-11-07
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "employeeName", value = "员工姓名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "employeeNo", value = "员工编号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "extension", value = "分机号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "currentPage", value = "页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, dataType = "Integer")
    })
    @ApiOperation(value = "分页查询数据")
    @GetMapping
    public IPage<EmployeeSearBindVO> getByPage(
            @RequestParam(value = "employeeName", required = false) String employeeName,
            @RequestParam(value = "employeeNo", required = false) String employeeNo,
            @RequestParam(value = "extension", required = false) String extension,
            @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
        Page<EmployeeSearBindVO> page = new Page(currentPage, pageSize);

        SearchBindParamVO vo = new SearchBindParamVO();
        vo.setEmployeeName(employeeName);
        vo.setEmployeeNo(employeeNo);
        vo.setExtension(extension);

        return employeeService.employeeSearchBind(page, vo);
    }

    /**
     * 根据ID获取数据
     *
     * @param id 数据主键ID
     * @return com.yonyou.cyx.cti.service.entity.dto.BindDTO
     * @author lijun
     * @since 2019-11-07
     */
    @ApiImplicitParam(name = "id", value = "绑定ID", required = false, dataType = "String", paramType = "path")
    @ApiOperation(value = "根据ID获取数据")
    @GetMapping(value = "/{id}")
    public BindDTO getById(@PathVariable("id") Long id) {
        return bindService.getById(id);
    }

    /**
     * 进行数据新增
     *
     * @param bindDTO 需要保存的DTO
     * @return int
     * @author lijun
     * @since 2019-11-07
     */
    @ApiImplicitParam(name = "bindDTO", value = "请求体", required = false, dataType = "BindDTO", paramType = "body")
    @ApiOperation(value = "进行数据新增")
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public int insert(@RequestBody BindDTO bindDTO) {
        return bindService.insert(bindDTO);
    }

    /**
     * 进行数据修改
     *
     * @param id      需要修改数据的ID
     * @param bindDTO 需要保存的DTO
     * @return int
     * @author lijun
     * @since 2019-11-07
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "绑定ID", required = false, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "bindDTO", value = "请求体", required = false, dataType = "BindDTO", paramType = "body")
    })
    @ApiOperation(value = "进行数据修改")
    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    public int update(@PathVariable("id") Long id, @RequestBody BindDTO bindDTO) {
        return bindService.update(id, bindDTO);
    }

    /**
     * 根据id删除对象
     *
     * @param id 需要修改数据的ID
     * @author lijun
     * @since 2019-11-07
     */
    @ApiImplicitParam(name = "id", value = "绑定ID", required = false, dataType = "String", paramType = "path")
    @ApiOperation(value = "根据id删除对象")
    @DeleteMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public boolean deleteById(@PathVariable("id") Long id) {
        bindService.deleteById(id);
        return true;
    }

    /**
     * 根据IDs批量删除对象
     *
     * @param ids 需要修改数据的ID集合
     * @author lijun
     * @since 2019-11-07
     */
    @ApiImplicitParam(name = "ids", value = "绑定IDs", required = false, dataType = "String", paramType = "path")
    @ApiOperation(value = "根据IDs批量删除对象")
    @DeleteMapping(value = "/batch/{ids}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public boolean deleteByIds(@PathVariable("ids") String ids) {
        bindService.deleteBatchIds(ids);
        return true;
    }

//    /**
//     * 查询手机号是否绑定分机号
//     *
//     * @param mobile 主叫手机号
//     * @return com.yonyou.cyx.cti.service.entity.dto.BindDTO
//     * @author lijun
//     * @since 2019-11-07
//     */
//    @ApiImplicitParam(name = "mobile", value = "主叫手机号", required = false, dataType = "String", paramType = "query")
//    @ApiOperation(value = "查询手机号是否绑定分机号")
//    @GetMapping(value = "/isBind")
//    public Boolean isBind(@RequestParam("mobile") String mobile) {
//        return bindService.isBind(mobile);
//    }

    @ApiOperation(value = "获取所有绑定")
    @GetMapping(value = "/getList")
    public List<BindPO> getList() {
        LambdaQueryWrapper<BindPO> wrapper = new QueryWrapper<BindPO>().lambda();
        List<BindPO> bindpos = bindService.list(wrapper);
        return bindpos;
    }

//    @ApiOperation(value = "根据手机号和AppId查询绑定")
//    @GetMapping(value = "/getByPhoneAndAppId")
//    public BindPO getByPhoneAndAppId(@RequestParam String caller, @RequestParam String appId) {
//        BindPO bindPO = bindService.getByPhoneAndAppId(caller, appId);
//        return bindPO;
//    }

    @ApiOperation(value = "根据手机号查询绑定")
    @GetMapping(value = "/getByPhone")
    public BindPO getByPhone(@RequestParam String mobile) {
        BindPO bindpo = bindService.getByPhone(mobile);
        return bindpo;
    }
}